package com.example.controller;

import com.example.common.ApiRestResponse;
import com.example.common.Constant;
import com.example.exception.ImoocMallExceptionEnum;
import com.example.mall.model.pojo.User;
import com.example.mall.model.request.UserInfo;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;
@RestController
@Api(tags = "User")
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    //查询
    @GetMapping("/selectName")
    @ApiOperation(value = "通过用户名查询信息", notes = "")
    public ApiRestResponse<Object> register(@RequestParam("username") String username) {
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        User user = userService.getUser(username);
        if (user == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NOEXIT_USER_NAME);
        }
        return ApiRestResponse.success(user);
    }

    //    注册
    @ApiOperation(value = "注册", notes = "")
    @GetMapping("/register")
    public ApiRestResponse register(@RequestParam("username") String username, @RequestParam("password") String password) {
/**
 * 判断用户名是否存在
 */

        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        if (password.length() < 8) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TEST);
        }
        userService.Register(username, password);
        return ApiRestResponse.success();
    }

    //    登录
    @ApiOperation(value = "登录", notes = "")
    @PostMapping("/login")
    public ApiRestResponse<Object> UserLogin(@RequestBody UserInfo userInfo, HttpSession session) {
        String username = userInfo.getUsername();
        String password = userInfo.getPassword();
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        if (password.length() < 8) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TEST);
        }

        User user = userService.Login(username, password);
        user.setPassword(null);
        session.setAttribute(Constant.Imooc_Mall_User, user);
        return ApiRestResponse.success(user);
    }

    ////    修改签名
    @ApiOperation(value = "更新签名", notes = "")
    @PostMapping("/updataSignature")
    public ApiRestResponse<Object> updataSignature(HttpSession session, @RequestBody UserInfo userInfo) {
        User uer = (User) session.getAttribute(Constant.Imooc_Mall_User);
        if (uer == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NO_LOGIN_USER);
        }
//       新建对象把修改后的内容赋值进去
        User user = new User();
        user.setId(uer.getId());
        user.setPassword(userInfo.getPassword());
        user.setUsername(userInfo.getUsername());
        user.setPersonalizedSignature(userInfo.getSignature());
        userService.updataSignature(user);
        return ApiRestResponse.success();
    }
}
